﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace AutoBackupDatabase.Form
{
    using AutoBackupDatabase.BLL;
    using AutoBackupDatabase.General;
    using AutoBackupDatabase.Report;

    using CrystalDecisions.CrystalReports.Engine;
    using CrystalDecisions.Shared;

    using DevExpress.XtraGrid.Columns;

    public partial class frmReport : DevExpress.XtraEditors.XtraForm
    {
        private List<ReportConfig> listReport;

        private ReportConfig reportConfig;
        public frmReport()
        {
            InitializeComponent();
            listReport=new List<ReportConfig>();
        }

        private void frmReport_Load(object sender, EventArgs e)
        {
            dtpFrom.EditValue = DateTime.Now.Date;
            dtpTo.EditValue = DateTime.Now.Date;
            this.BuildListReportSource();
        }

        private void BuildListReportSource()
        {
            this.reportConfig = new ReportConfig();
            this.reportConfig.ReportId = "BAOCAODOANHTHUTHEOHOADON";
            this.reportConfig.ReportName = "Báo cáo doanh thu theo hóa đơn";
            this.listReport.Add(this.reportConfig);

            this.reportConfig = new ReportConfig();
            this.reportConfig.ReportId = "BAOCAODOANHTHUTHEOMONAN";
            this.reportConfig.ReportName = "Báo cáo doanh thu theo món ăn";
            this.listReport.Add(this.reportConfig);

            this.reportConfig = new ReportConfig();
            this.reportConfig.ReportId = "BAOCAODOANHTHUTHEONGAY";
            this.reportConfig.ReportName = "Báo cáo doanh thu theo ngày";
            this.listReport.Add(this.reportConfig);

            this.reportConfig = new ReportConfig();
            this.reportConfig.ReportId = "BaoCaoTongHop";
            this.reportConfig.ReportName = "Báo cáo doanh thu tổng hợp";
            this.listReport.Add(this.reportConfig);

            this.glkReportType.Properties.DataSource = this.listReport;
            this.glkReportType.Properties.DisplayMember = "ReportName";
            this.glkReportType.Properties.ValueMember = "ReportId";

            GridColumn col2 = glkReportType.Properties.View.Columns.AddField("ReportName");
            col2.VisibleIndex = 0;
            col2.Caption = "Tên loại báo cáo";

            // Set column widths according to their contents.
            glkReportType.Properties.View.BestFitColumns();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {

            ParameterFields paraFileds = new ParameterFields();
            ParameterField paraFiled = new ParameterField();
            ParameterDiscreteValue paraDescritValue;
            ParameterField paraFiled1 = new ParameterField();
            ParameterDiscreteValue paraDescritValue1;
            try
            {
                if (glkReportType.EditValue.ToString() == "BAOCAODOANHTHUTHEOMONAN")
                {
                    BAOCAODOANHTHUTHEOMONAN rptRR = new BAOCAODOANHTHUTHEOMONAN();
                    ReportBLL reportBll = new ReportBLL();
                    var dt = reportBll.BAOCAODOANHTHUTHEOMONAN(DateTime.Parse(dtpFrom.EditValue.ToString()), DateTime.Parse(dtpTo.EditValue.ToString()).Date);
                    rptRR.Database.Tables["BAOCAODOANHTHUTHEOMONAN"].SetDataSource(dt);
                    paraFiled.Name = "from";
                    paraFiled.CurrentValues.Clear();
                    paraDescritValue = new ParameterDiscreteValue();
                    paraDescritValue.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpFrom.EditValue.ToString()));
                    paraFiled.CurrentValues.Add(paraDescritValue);
                    paraFileds.Add(paraFiled);
                    paraFiled1.Name = "to";
                    paraFiled1.CurrentValues.Clear();
                    paraDescritValue1 = new ParameterDiscreteValue();
                    paraDescritValue1.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpTo.EditValue.ToString()));
                    paraFiled1.CurrentValues.Add(paraDescritValue1);
                    paraFileds.Add(paraFiled1);

                    crystalReportViewer1.ParameterFieldInfo = paraFileds;

                    crystalReportViewer1.ReportSource = rptRR;
                    crystalReportViewer1.RefreshReport();
                    return;
                }

                if (glkReportType.EditValue.ToString() == "BAOCAODOANHTHUTHEOHOADON")
                {
                    BAOCAODOANHTHUTHEOHOADON rptRR = new BAOCAODOANHTHUTHEOHOADON();
                    ReportBLL reportBll = new ReportBLL();
                    var dt = reportBll.BAOCAODOANHTHUTHEOHOADON(DateTime.Parse(dtpFrom.EditValue.ToString()), DateTime.Parse(dtpTo.EditValue.ToString()).Date);
                    rptRR.Database.Tables["BAOCAODOANHTHUTHEOHOADON"].SetDataSource(dt);
                    paraFiled.Name = "from";
                    paraFiled.CurrentValues.Clear();
                    paraDescritValue = new ParameterDiscreteValue();
                    paraDescritValue.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpFrom.EditValue.ToString()));
                    paraFiled.CurrentValues.Add(paraDescritValue);
                    paraFileds.Add(paraFiled);
                    paraFiled1.Name = "to";
                    paraFiled1.CurrentValues.Clear();
                    paraDescritValue1 = new ParameterDiscreteValue();
                    paraDescritValue1.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpTo.EditValue.ToString()));
                    paraFiled1.CurrentValues.Add(paraDescritValue1);
                    paraFileds.Add(paraFiled1);

                    crystalReportViewer1.ParameterFieldInfo = paraFileds;

                    crystalReportViewer1.ReportSource = rptRR;
                    crystalReportViewer1.RefreshReport();
                    return;
                }

                if (glkReportType.EditValue.ToString() == "BAOCAODOANHTHUTHEONGAY")
                {
                    BAOCAODOANHTHUTHEONGAY rptRR = new BAOCAODOANHTHUTHEONGAY();
                    ReportBLL reportBll = new ReportBLL();
                    var dt = reportBll.BAOCAODOANHTHUTHEONGAY(DateTime.Parse(dtpFrom.EditValue.ToString()), DateTime.Parse(dtpTo.EditValue.ToString()).Date);
                    rptRR.Database.Tables["BAOCAODOANHTHUTHEONGAY"].SetDataSource(dt);
                    paraFiled.Name = "from";
                    paraFiled.CurrentValues.Clear();
                    paraDescritValue = new ParameterDiscreteValue();
                    paraDescritValue.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpFrom.EditValue.ToString()));
                    paraFiled.CurrentValues.Add(paraDescritValue);
                    paraFileds.Add(paraFiled);
                    paraFiled1.Name = "to";
                    paraFiled1.CurrentValues.Clear();
                    paraDescritValue1 = new ParameterDiscreteValue();
                    paraDescritValue1.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpTo.EditValue.ToString()));
                    paraFiled1.CurrentValues.Add(paraDescritValue1);
                    paraFileds.Add(paraFiled1);

                    crystalReportViewer1.ParameterFieldInfo = paraFileds;

                    crystalReportViewer1.ReportSource = rptRR;
                    crystalReportViewer1.RefreshReport();
                    return;
                }

                if (glkReportType.EditValue.ToString() == "BaoCaoTongHop")
                {
                    BaoCaoTongHop rptRR = new BaoCaoTongHop();
                    ReportBLL reportBll = new ReportBLL();
                    var dt = reportBll.BAOCAOTONGHOP(DateTime.Parse(dtpFrom.EditValue.ToString()), DateTime.Parse(dtpTo.EditValue.ToString()).Date);
                    rptRR.Database.Tables["BaoCaoTongHop"].SetDataSource(dt);
                    paraFiled.Name = "from";
                    paraFiled.CurrentValues.Clear();
                    paraDescritValue = new ParameterDiscreteValue();
                    paraDescritValue.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpFrom.EditValue.ToString()));
                    paraFiled.CurrentValues.Add(paraDescritValue);
                    paraFileds.Add(paraFiled);
                    paraFiled1.Name = "to";
                    paraFiled1.CurrentValues.Clear();
                    paraDescritValue1 = new ParameterDiscreteValue();
                    paraDescritValue1.Value = string.Format("{0:dd MM yyyy}", DateTime.Parse(dtpTo.EditValue.ToString()));
                    paraFiled1.CurrentValues.Add(paraDescritValue1);
                    paraFileds.Add(paraFiled1);

                    crystalReportViewer1.ParameterFieldInfo = paraFileds;

                    crystalReportViewer1.ReportSource = rptRR;
                    crystalReportViewer1.RefreshReport();
                    
                }
            }
            catch (Exception)
            {
                    
                
            }


          
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                crystalReportViewer1.PrintReport();
            }
            catch (Exception)
            {
                    
               
            }
            
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                crystalReportViewer1.ExportReport();
            }
            catch (Exception)
            {
                    
                
            }
           
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        


 

        
    }
}